package com.company.woye.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by KangJunjie on 2020/6/19.
 */
public class DataUtils {
    /**
     * 年月日
     */
    public final static String PATTERN_DATE = "yyyy-MM-dd";
    /**
     * 年月日时分秒
     */
    public final static String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";

    /**
     * 解析字符串
     *
     * @param date
     * @return
     */
    public static Date parse(String date) {
        SimpleDateFormat format = new SimpleDateFormat(PATTERN_DATETIME);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 获取网络时间
     */
    public  static String getNowTime(){
        URL url= null;//取得资源对象
        String time="";
        try {
            url = new URL("http://www.bjtime.cn");
            URLConnection uc= null;//生成连接对象
            try {
                uc = url.openConnection();
                uc.connect(); //发出连接
            } catch (IOException e) {
                e.printStackTrace();
            }

            long ld=uc.getDate(); //取得网站日期时间
            Date date=new Date(ld); //转换为标准时间对象
            //分别取得时间中的小时，分钟和秒，并输出
            time=date.getYear()+"-"+date.getMonth()+"-"+date.getDay()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+"";
            return time;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 解析字符串
     *
     * @param date
     * @return
     */
    public static Date parse(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String getCurrentTime() {

        return null;
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        return format(date, PATTERN_DATETIME);
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String format2Date(Date date) {
        return format(date, PATTERN_DATE);
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date == null) return "";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制)<br>
     * 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21'<br>
     * @return String   字符串<br>
     */


    public static String dateToString(Date data){
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat(PATTERN_DATE);
        String time = formatter.format(data);
        return time;
    }
    public static String listToString(List list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(",");
        }
        return sb.toString().substring(0,sb.toString().length()-1);
    }
    public static String listToStringFenhao(List list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(";");
        }
        return sb.toString().substring(0,sb.toString().length()-1);
    }
    public static String listToStringPoint(List list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append("'"+list.get(i)+"'").append(",");
        }
        return sb.toString().substring(0,sb.toString().length()-1);
    }
    public  static String getNowData(){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_DATE);//设置日期格式
        String createTime = dateFormat.format(now);//格式化然后放入字符串中
        return  createTime;
    }
}
